package com.openharmony.filepicker.adapter;

import com.openharmony.filepicker.ResourceTable;
import ohos.agp.components.*;
import ohos.app.Context;

import java.util.ArrayList;
import java.util.List;

public class SampleProvider extends BaseItemProvider {

    private List<String> data = new ArrayList<>();
    private Context context;

    public SampleProvider(Context context) {
        this.context = context;
    }

    public void setInfo(List<String> data) {
        this.data.clear();
        if (data != null) {
            this.data.addAll(data);
        }
        notifyDataChanged();
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public Component getComponent(int i, Component component, ComponentContainer componentContainer) {
        Component cpt;
        if (component == null) {
            cpt= LayoutScatter.getInstance(context).parse(ResourceTable.Layout_item_result, null, false);
            cpt.setTag(new ViewHolder(cpt));
        } else {
            cpt = component;
        }

        ViewHolder viewHolder = (ViewHolder) cpt.getTag();
        viewHolder.text.setText(data.get(i));

        System.out.println("=== get component string : " + data.get(i));

        return cpt;
    }

    private class ViewHolder {

        public Text text;

        public ViewHolder(Component component) {
            text = (Text) component.findComponentById(ResourceTable.Id_item_result_path);
        }
    }

}
